package com.zicheng.util;

import com.easyooo.framework.common.util.DateUtils;

import java.util.Date;

public class DateCommonUtil {

    /**
     * 2025-03-27 麦时4月改为周麦时
     * 月麦时 和 周麦时 区别最主要在于 日期的选择
     * 当出现月麦时和周麦时的变动，时间变动的处理比较耗时
     **/
    public static Date getStartTime(MaiShiTimeEnum maiShiTimeEnum,Date now) {
        Date start = null;
        Date lastMonth = DateUtils.plusMonths(now, -1);
        Date lastEndOfMonth;
        switch (maiShiTimeEnum) {
            case Week:
            case Month2Week:
                start = DateUtils.getFirstOfWeek(now);
                lastEndOfMonth = DateUtils.getEndOfMonth(lastMonth);
                return calculateStartTime(lastEndOfMonth,start);
            case Month:
            case Week2Month:
                start = DateUtils.getFirstOfMonth(now);
                lastEndOfMonth = DateUtils.getEndOfMonth(lastMonth);
                Date lastEndOfWeek = DateUtils.getEndOfWeek(lastEndOfMonth);
                return calculateStartTime(lastEndOfWeek,start);
        }
        return start;
    }

    private static Date calculateStartTime(Date last,Date now){
        if(last.getTime()<now.getTime()){
            return now;
        }
        return last;
    }

    /**
     * 结算时间变更
     *
     * @return
     */
    public static Date getEndTime(MaiShiTimeEnum maiShiTimeEnum) {
        switch (maiShiTimeEnum) {
            case Week:
            case Month2Week:
                return DateUtils.getEndOfWeek();
            case Month:
            case Week2Month:
                return DateUtils.getEndOfMonth();
        }
        return null;
    }

    /**
     * 麦时不同时间计算方式枚举
     **/
    public enum MaiShiTimeEnum {
        Month,
        Week,
        Week2Month,
        Month2Week;
    }
}
